package com.hph.springcloudhystrixclientdemo.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * @author {USER}
 * @date {date}
 */
@RestController
public class HystrixDemoController {

    private Random random;

    /**
     * fallbackMethod：当helloWorld方法调用失败时，会调用errorContent方法替代返回
     * HystrixProperty：熔断的规则，这里当线程的等待时间超过100秒时，直接返回失败
     * @return String
     */
    @GetMapping(value = "/hello-world")
    @HystrixCommand(fallbackMethod = "errorContent",
            commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "100")})
    public String helloWorld() throws Exception {

        random = new Random();

        Thread.sleep(random.nextInt(200));

        return "Hello World";
    }

    public String errorContent(){

        return "error";
    }
}
